/********************************************************************************
* File Name:	myitem.cpp
* Description:	第14章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "myitem.h"
#include <QtGlobal>
#include <time.h>
//9 items array
unsigned char item1[4*4]=
{
1,1,1,0,
1,0,1,0,
1,1,1,0,
0,0,1,0
};
unsigned char item2[4*4]=
{
0,0,0,0,
1,1,1,0,
0,1,0,0,
0,1,0,0
};

unsigned char item3[4*4]=
{
0,0,0,0,
1,0,1,0,
1,1,1,0,
0,0,1,0
};
unsigned char item4[4*4]=
{
0,0,1,1,
0,0,0,1,
0,0,1,1,
0,0,0,1
};
unsigned char item5[4*4]=
{
0,0,0,0,
0,0,1,0,
0,0,0,0,
0,0,0,0
};
unsigned char item6[4*4]=
{
0,0,0,0,
0,0,0,0,
0,1,1,0,
0,0,0,0
};

unsigned char item7[4*4]=
{
1,0,1,0,
1,1,1,0,
0,0,0,0,
0,0,0,0
};

unsigned char item8[4*4]=
{
0,0,0,0,
0,1,1,0,
0,1,0,0,
0,0,0,0
};

unsigned char item9[4*4]=
{
0,0,1,0,
0,0,1,0,
0,0,1,0,
0,0,1,0
};

MyItem::MyItem()
{
    qsrand(time(0));    //select random
}
unsigned char* MyItem::getItem()
{
    unsigned char *currentItem=NULL;
    switch(qrand()%9)   //select one item random
    {
        case 0 : currentItem = item1;break;
        case 1 : currentItem = item2;break;
        case 2 : currentItem = item3;break;
        case 3 : currentItem = item4;break;
        case 4 : currentItem = item5;break;
        case 5 : currentItem = item6;break;
        case 6 : currentItem = item7;break;
        case 7 : currentItem = item8;break;
        case 8 : currentItem = item9;break;
    }
    return currentItem;
}

int MyItem::currentItem_endPos(unsigned char *currentItem,unsigned char end)
        //current item's position at 4*4 grid
{
    int k = 0;
    switch(end)
    {
        case 'd' :   //count rows from top
        {
            for(int row=3;row>=0;row--)
                for(int col=0;col<4;col++)
                {
                    if(*(currentItem+row*4+col) || k ==1)
                        return row;
                }
        }
        case 'r' :  //count end cols from left
        {
            for(int col=3;col>=0;col--)
                for(int row=0;row<4;row++)
                {
                if(*(currentItem+row*4+col) || k ==1)
                        return col;
                }
        }
        case 'l' :  //count begin cols from left
        {
            for(int col=0;col<4;col++)
                for(int row=0;row<4;row++)
                {
                    if(*(currentItem+row*4+col) || k ==1)
                        return col;
                }
        }
        default: return 0;
    }
}

unsigned char* MyItem::itemChange(unsigned char *currentItem)//change item
{
    unsigned char* tempItem = new unsigned char[4*4];
     //use temp item
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        {
            *(tempItem +(3-j)*4+i)=*(currentItem +i*4+j);    // rotate 90 anticlockwise,put it to temp item
        }
    return tempItem;
}
